package com.cuhk.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.cuhk.common.exception.BizCodeEnum;
import com.cuhk.gulimall.member.exception.NoMemberFoundException;
import com.cuhk.gulimall.member.exception.NoUniqueMemberNameException;
import com.cuhk.gulimall.member.exception.NoUniqueMemberPhoneException;
import com.cuhk.gulimall.member.exception.PasswordNotFoundException;
import com.cuhk.gulimall.member.feign.CouponFeignService;
import com.cuhk.gulimall.member.vo.MemberLoginVo;
import com.cuhk.gulimall.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cuhk.gulimall.member.entity.MemberEntity;
import com.cuhk.gulimall.member.service.MemberService;
import com.cuhk.common.utils.PageUtils;
import com.cuhk.common.utils.R;



/**
 * 会员
 *
 * @author chenyujing
 * @email torchsam@aliyun.com
 * @date 2024-06-10 14:39:46
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @PostMapping("/save-member")
    public R saveMember(@RequestBody MemberRegisterVo memberRegisterVo) {
        try {
            memberService.saveMember(memberRegisterVo);
        } catch (NoUniqueMemberNameException ne) {
            return R.error(BizCodeEnum.NAME_ALREADY_EXIST_EXCEPTION.getCode(), BizCodeEnum.NAME_ALREADY_EXIST_EXCEPTION.getMessage());
        } catch (NoUniqueMemberPhoneException pe) {
            return R.error(BizCodeEnum.PHONE_ALREADY_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_ALREADY_EXIST_EXCEPTION.getMessage());
        }
        return R.ok();
    }

    @PostMapping("/login-member")
    public R loginMember(@RequestBody MemberLoginVo memberLoginVo) {
       try {
           memberService.loginMember(memberLoginVo);
       } catch (NoMemberFoundException nm) {
           return R.error(BizCodeEnum.NAME_FOUND_FAILED_EXCEPTION.getCode(), BizCodeEnum.NAME_FOUND_FAILED_EXCEPTION.getMessage());
       } catch (PasswordNotFoundException pn) {
           return R.error(BizCodeEnum.PASSWORD_MATCH_FAILED_EXCEPTION.getCode(), BizCodeEnum.PASSWORD_MATCH_FAILED_EXCEPTION.getMessage());
       }
       return R.ok();
    }

    @RequestMapping("/coupons")
    public R test () {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R memberCoupons = couponFeignService.memberCoupons();
        return R.ok().put("member", memberEntity).put("coupons", memberCoupons.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
